package org.lab.tracecash.common.security;

import java.security.Principal;

import javax.inject.Singleton;

@Singleton
public class SecurityService {

	private ThreadLocal<Principal> currentUser = new ThreadLocal<Principal>();

	public Principal getCurrentUser() {
		Principal user = currentUser.get();
		return user != null ? user : buildSystemUser();
	}

	public void setCurrentUser(Principal value) {
		currentUser.set(value);
	}

	public void setDefaulySystemUser() {
		setCurrentUser(buildSystemUser());
	}

	private Principal buildSystemUser() {
		return new Principal() {

			@Override
			public String getName() {
				return "SYSTEM";
			}
		};
	}
}
